import { reqGetStaffList, reqGetStaffListByNumber, reqGetStaffNumber } from '@/api/staff'
// import router from '@/router'
export default {
  namespaced: true,
  state: () => ({
    staffList: [],
    pagination: {
      currentPage: 1,
      pageSize: 5,
      total: 0
    },
    staff_newNumber: null,
    staff: [],
    addOrMod: true
  }),
  mutations: {
    GETSTAFFLIST(state, staffList) {
      state.staffList = [...staffList]
    },
    // 保存分页数据
    SAVEPAGINATION(state, pagination) {
      state.pagination.currentPage = pagination.currentPage
      state.pagination.pageSize = pagination.pageSize
      state.pagination.total = pagination.total
    },
    // 保存添加员工工号的值
    SAVESTAFFNUMBER(state, newNumber) {
      state.staff_newNumber = newNumber
    },
    // 保存修改员工的数据
    SAVESTAFF(state, staff) {
      state.staff = { ...staff }
    },
    // 保存addOrMod
    SAVEADDORMOD(state, addOrMod) {
      state.addOrMod = addOrMod
    }
  },
  actions: {
    // 获取员工列表
    async getStaffList({ commit }, { search, pagination }) {
      const { currentPage, pageSize } = pagination.value
      let result = null
      if (search === '' || search === undefined || search === null) {
        result = await reqGetStaffList({ currentPage, pageSize })
      } else {
        result = await reqGetStaffListByNumber({ search: search * 1, currentPage, pageSize })
      }
      if (result.code === 200) {
        commit('GETSTAFFLIST', result.data.data)
        commit('SAVEPAGINATION', {
          currentPage,
          pageSize,
          total: result.data.length
        })
        return 'OK'
      } else {
        return Promise.resolve(new Error('出错了！'))
      }
    },
    // 获取最后员工工号
    async getStaffNumer({ commit }) {
      const result = await reqGetStaffNumber()
      if (result.code === 200) {
        commit('SAVESTAFFNUMBER', result.data + 1)
        return 'OK'
      } else {
        return Promise.resolve(new Error('出错了！'))
      }
    }
  }
}
